package com.example.sunsg.okhttptest.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.breadtrip.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.module.GlideModule;
import com.example.sunsg.okhttptest.MainActivity;
import com.example.sunsg.okhttptest.cuseomannotation.ViewAnnotation;
import com.example.sunsg.okhttptest.util.AnnotationUtil;

import java.io.File;
import java.util.concurrent.atomic.AtomicBoolean;

import jp.wasabeef.glide.transformations.BlurTransformation;

public class GlideActivity extends AppCompatActivity {
    @ViewAnnotation(R.id.iv_glide_1)
    ImageView ivGlide1;
    public static void launch(Activity activity){
        Intent intent = new Intent(activity,GlideActivity.class);
        activity.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_glide);
        AnnotationUtil.injectionView(this);
//        String   url = "https://img.gcall.com/dca5/M00/10/8E/wKhoNlggetaENWylAAAAAAAAAAA457.jpg";
//        //加载图片
//        Glide.with(GlideActivity.this).load(url).placeholder(R.drawable.beauty).error(R.drawable.ren).dontAnimate().into(ivGlide1);
        //加载gif图
//        Glide.with(this).load(R.drawable.huangbo).diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(R.drawable.beauty).into(ivGlide1);
        //override 压缩到接近 300 * 300
//        Glide.with(this).load(mUrl).override(300,300).into(mIv);
//        Glide.with(this).load(R.drawable.ren).placeholder(R.drawable.beauty).into(ivGlide1);
//        Glide.get(this).clearDiskCache();
//        File file = new File("com/example/sunsg/okhttptest/cache/image_manager_disk_cache/");
//        file = getCacheDir();
//        if(file != null){
//            if(file.isDirectory()){
//                File[] files = file.listFiles();
//                for (File file1 : files){
//                    Log.i("test","file.path = "+ file1.getAbsolutePath());
//                }
//            }
//        }

        Glide.with(this)
                .load(R.drawable.ren)
                .crossFade(1000)
                .bitmapTransform(new BlurTransformation(this,23,1)) // “23”：设置模糊度(在0.0到25.0之间)，默认”25";"4":图片缩放比例,默认“1”。
                .into(ivGlide1);
//        ivGlide1.getDrawable().
        ivGlide1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GlideActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });


    }

    public class MyGlideClassMoudle implements GlideModule{
        //在清单文件中添加 自己定义的给力的module
//        <application>
//        ...
//
//        <meta-data
//        android:name="MyGlideClassMoudle"
//        android:value="GlideModule" />
//        </application>
        @Override
        public void applyOptions(Context context, GlideBuilder builder) {
//            builder.setDiskCache();//自定义磁盘缓存

//            builder.setMemoryCache();//自定义内存缓存

//            builder.setBitmapPool(); //自定义图片池

//            builder.setDiskCacheService();//自定义本地缓存的线程池

//            builder.setResizeService();//自定义核心处理的线程池

//            builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);//自定义图片质量

            //配置内存缓存大小 10MB
//            builder.setMemoryCache(new LruResourceCache(10*1024*1024));
            //配置图片池大小   20MB
//            builder.setBitmapPool(new LruBitmapPool(20*1024*1024));
        }

        @Override
        public void registerComponents(Context context, Glide glide) {

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("main","glide ondestroy");
    }
}
